// AWTButtonPeer.cpp : implementation file
//

#include "stdafx.h"
//#include "AWTTest.h"
#include "AWTButtonPeer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTButtonPeer, CButton)

/////////////////////////////////////////////////////////////////////////////
// AWTButtonPeer

AWTButtonPeer::AWTButtonPeer(AWTComponent* comp)
{
	m_comp = comp;
	m_AWTWindow = NULL;
}

AWTButtonPeer::~AWTButtonPeer()
{
//	if (m_hWnd != NULL)
//		DestroyWindow();
	if (m_hWnd != NULL)
	{
		HWND hwnd = m_hWnd;
		::DestroyWindow(hwnd);
		Detach();
	}
}


BEGIN_MESSAGE_MAP(AWTButtonPeer, CButton)
	//{{AFX_MSG_MAP(AWTButtonPeer)
	ON_WM_SIZE()
	ON_WM_MOVE()
	ON_WM_SHOWWINDOW()
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	ON_WM_KEYDOWN()
	ON_WM_CHAR()
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	ON_WM_DESTROY()
	ON_WM_GETDLGCODE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void AWTButtonPeer::setDefault(BOOL b) 
{
	if (b)
		ModifyStyle(0, BS_DEFPUSHBUTTON);
	else
		ModifyStyle(BS_DEFPUSHBUTTON, 0);
	InvalidateRect(NULL);
}

/////////////////////////////////////////////////////////////////////////////
// AWTButtonPeer message handlers

void AWTButtonPeer::OnSize(UINT nType, int cx, int cy) 
{
	CButton::OnSize(nType, cx, cy);
	
	AWTComponentEvent e(m_comp, AWTComponentEvent::COMPONENT_RESIZED);
	m_comp->processComponentEvent(&e);
}

void AWTButtonPeer::OnMove(int x, int y) 
{
	CButton::OnMove(x, y);
	
	AWTComponentEvent e(m_comp, AWTComponentEvent::COMPONENT_MOVED);
	m_comp->processComponentEvent(&e);
}

void AWTButtonPeer::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CButton::OnShowWindow(bShow, nStatus);
	if (bShow)	
	{
		AWTComponentEvent e(m_comp, AWTComponentEvent::COMPONENT_SHOWN);
		m_comp->processComponentEvent(&e);
	}
	else
	{
		AWTComponentEvent e(m_comp, AWTComponentEvent::COMPONENT_HIDDEN);
		m_comp->processComponentEvent(&e);
	}
}

void AWTButtonPeer::OnClicked() 
{
	CString s = ((AWTButton*)m_comp)->getLabel();
	AWTActionEvent e(m_comp, AWTActionEvent::ACTION_PERFORMED, &s);
	((AWTButton*)m_comp)->processActionEvent(&e);
}

void AWTButtonPeer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{

	if (nChar == VK_RETURN)
	{
		TRACE(_T("AWTButtonPeer::OnKeyDown\n"));
		OnClicked();
		return;
	}

	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_PRESSED, (long)t, nFlags, 0, nChar);
	m_comp->processKeyEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);
	CButton::OnKeyDown(nChar, nRepCnt, nFlags);
}

void AWTButtonPeer::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (nChar == '\t')
		return;
	
	CButton::OnChar(nChar, nRepCnt, nFlags);
}

int AWTButtonPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CButton::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);

/*
	CWnd* wnd = this;
	CString str;
	TCHAR szClass[128];
	BOOL bFound = FALSE;
	do {
		if ((wnd = wnd->GetParent()) == NULL)
			break;
		wnd->GetWindowText(str);
		::GetClassName(wnd->m_hWnd, szClass, 128);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
		else if (CString(szClass) == CString("#32770"))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/	
	return 0;
}

void AWTButtonPeer::OnSetFocus(CWnd* pOldWnd) 
{
	TRACE(_T("AWTButtonPeer::OnSetFocus\n"));

	CButton::OnSetFocus(pOldWnd);

	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTButtonPeer::OnKillFocus(CWnd* pNewWnd) 
{
	TRACE(_T("AWTButtonPeer::OnKillFocus\n"));

	CButton::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTButtonPeer::OnDestroy() 
{
	CButton::OnDestroy();
	AWTWindowEvent e(m_comp, AWTWindowEvent::WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}

UINT AWTButtonPeer::OnGetDlgCode() 
{
	return DLGC_WANTTAB;
}
